package net.learn2develop.SMSMessaging;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SMSDecrypt extends Activity {
	private static final int REQUEST_CODE = 0;
	EditText txtEncrypted;
	EditText txtDecrypted;
	EditText txtPassword;
	Button btnDecrypt;
	
	private AESCrypto aesCrypt;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.decrypt);

		btnDecrypt = (Button) findViewById(R.id.btnDecrypt);
		txtEncrypted = (EditText) findViewById(R.id.txtEncrypted);
		txtDecrypted = (EditText) findViewById(R.id.txtDecrypted);
		txtPassword = (EditText) findViewById(R.id.txtPassword);

		String encrypted = "";
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			encrypted = extras.getString("encrypted");
			txtEncrypted.setText(encrypted, TextView.BufferType.EDITABLE);
		}

		btnDecrypt.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String encrypted = txtEncrypted.getText().toString();
				String password = txtPassword.getText().toString();
				try {
					if (password.length() > 0) {
						if (aesCrypt == null) aesCrypt = new AESCrypto();
						byte[] encryptedByte = encrypted.getBytes();
						String decrypted =new String(aesCrypt.Decrypt(encryptedByte.length, encryptedByte, password), "UTF-8");
						//String decrypted = SimpleCrypto.decrypt(password, encrypted);
						txtDecrypted.setText(decrypted,
								TextView.BufferType.EDITABLE);
					} else
						Toast.makeText(getBaseContext(),
								"Please enter password.", Toast.LENGTH_SHORT)
								.show();
				} catch (Exception e) {
					Toast.makeText(getBaseContext(),
							e.getMessage(), Toast.LENGTH_SHORT)
							.show();
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	public void onClickGetSMS(View view) {
		Intent i = new Intent(this, SMSRead.class);

		startActivityForResult(i, REQUEST_CODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra(Constants.ENCRYPTED_RETURN_VALUE)) {
				
				txtEncrypted.setText(data.getExtras().getString(Constants.ENCRYPTED_RETURN_VALUE));
			}
		}
	}
	
	
}
